package xyz.xmh.pattern.structural.adapterpattern;

/**
 * MediaAdapter
 * 实现MediaPlay接口的适配器
 *
 * @author xuMingHai
 * @since 2021/4/29 星期四 18:28
 */
public class MediaAdapter implements MediaPlayer {

    /**
     * 文件类型
     */
    private static final String VLC = "vlc";
    private static final String MP4 = "mp4";
    /**
     * 高级播放器接口
     */
    private AdvancedMediaPlayer advancedMediaPlayer;

    /**
     * 根据类型生成播放器
     *
     * @param audioType 音频类型
     */
    public MediaAdapter(String audioType) {
        if (audioType.equalsIgnoreCase(VLC)) {
            advancedMediaPlayer = new VlcPlayer();
        }
        if (audioType.equalsIgnoreCase(MP4)) {
            advancedMediaPlayer = new Mp4Player();
        }
    }

    /**
     * 根据不同类型调用不同的对象，模仿多态
     *
     * @param audioType 音频类型
     * @param fileName  文件类型
     */
    @Override
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase(VLC)) {
            advancedMediaPlayer.playVlc(fileName);
        }
        else if (audioType.equalsIgnoreCase(MP4)) {
            advancedMediaPlayer.playMp4(fileName);
        }
    }

}
